package com.hnit.controllers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hnit.configs.OrderCreateRequest;
import com.hnit.service.OrdersService;
import com.hnit.web.DTO.OrdersDTO;
import com.hnit.web.Utils.ResponseResult;
import lombok.AllArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/orders/operation")
@AllArgsConstructor
public class OrdersController {

    private final OrdersService ordersService;

    @PostMapping("/create")
    public ResponseEntity<ResponseResult> createOrder(
            @RequestHeader("X-User-ID") Long userId,
            @RequestBody OrderCreateRequest request) { // 接收新的请求体对象
        try {
            OrdersDTO order = ordersService.createOrder(userId, request.getOrdersDTO(), request.getOrderItemsDTOList(), request.getCartIds());
            return new ResponseEntity<>(ResponseResult.ok("订单创建成功").setData(order), HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(ResponseResult.error("订单创建失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/list")
    public ResponseEntity<ResponseResult> getOrdersByUserIdAndStatus(
            @RequestHeader("X-User-ID") Long userId,
            @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            // 调用服务层方法查询订单
            List<OrdersDTO> orders = ordersService.getOrdersByUserIdAndStatus(userId, orderStatus, page, size);
            return new ResponseEntity<>(ResponseResult.ok("查询成功").setData(orders), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(ResponseResult.error("查询失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //管理员获取所有订单记录，分页查询
    //管理员获取所有订单记录，分页查询
    @GetMapping("/list/all")
    public ResponseEntity<ResponseResult> getOrdersList(
            @RequestParam Integer page,
            @RequestParam Integer size,
            @RequestParam(required = false) Long orderId,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Integer orderStatus,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime paymentStartTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime paymentEndTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime shippingStartTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime shippingEndTime) {

        IPage<OrdersDTO> pageResult = ordersService.getAllOrders(
                page, size, orderId, userId, orderStatus,
                paymentStartTime, paymentEndTime,
                shippingStartTime, shippingEndTime
        );
        return ResponseEntity.ok(ResponseResult.ok("查询成功").setData(pageResult));
    }

    //管理员更新订单状态
    @PutMapping("/update/status")
    public ResponseEntity<ResponseResult> updateOrderStatus(
            @RequestParam Long orderId,
            @RequestParam Integer orderStatus) {
        try {
            ordersService.updateOrderStatus(orderId, orderStatus);
            return new ResponseEntity<>(ResponseResult.ok("订单状态更新成功"), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(ResponseResult.error("订单状态更新失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
